#!/bin/bash

set -e

rm -f programs.dep

ghc -M -dep-makefile programs.dep "$@"

sed -i 's:\([a-zA-Z]*\.\(o\|hi\)\):build/\1:g' programs.dep

for PROGRAM_HS in "$@"; do
    PROGRAM=$(echo ${PROGRAM_HS} | sed 's/\.hs//')
    OBJECTS=$(grep ${PROGRAM}.o programs.dep | grep .hi | awk '{ print $3 }' | sed 's/\.hi/.o/')
    echo build/${PROGRAM}: ${OBJECTS} build/${PROGRAM}.o >> programs.dep
    echo '	ghc -o $@ $^' >> programs.dep
done
